package com.sfh.tourist;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.DialogBuilder;
import com.sfh.lib.ui.MVVMActivity;
import com.sfh.tourist.base.ImageLoaderEngine;
import com.sfh.tourist.base.ImageSelectProgressView;
import com.sfh.tourist.base.ImageSelector;
import com.sfh.tourist.base.PermissionUtil;
import com.sfh.tourist.base.ProcessFile;

import java.util.List;

public class AddInfoActivity extends MVVMActivity {

    private EditText etMunber;
    private EditText etName;
    private EditText et_type;
    private EditText etMonomerLevel;
    private EditText etBaseTypeName;
    private EditText etMainName;
    private EditText etSubclassName;
    private TextView tvLocation;
    private TextView etAddress;
    private EditText etDevLevel;
    private EditText etScenicSpot;
    private EditText etTraffic;
    private EditText etMeasureArea;
    private EditText etMeasureStatus;
    private ImageSelectProgressView ivImages;
    private EditText etInfo;
    private TextView tvOk;

    private ReqAddInfo reqAddInfo = new ReqAddInfo();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_info_activity);
        initView();

        tvLocation.setOnClickListener(view -> PermissionUtil.checkPermissions(AddInfoActivity.this, "请开启定位权限", 100, new PermissionUtil.PermissionCallBack() {
            @Override
            public void onHasPermission() {
                Intent intent = new Intent(AddInfoActivity.this, GDAmapActivity.class);
                startActivityForResult(intent, 1002);
            }

            @Override
            public void onFailedPermission() {

            }
        }, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION));

        ivImages.setOnItemSelectListener(new ImageSelectProgressView.OnItemSelectListener() {
            @Override
            public void onSelectImageClick(int surplus) {
                PermissionUtil.checkPermissions(AddInfoActivity.this, "请允许访问相册与摄像头", 1011, new PermissionUtil.PermissionCallBack() {
                    @Override
                    public void onHasPermission() {
                        ImageSelector.create().count(surplus).showCamera(true).start(AddInfoActivity.this, 1010);
                    }

                    @Override
                    public void onFailedPermission() {
                        showDialogToast("授权失败！");
                    }
                }, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
            }

            @Override
            public void onUpImageClick(String file) {
                getViewModel(TouristModel.class).onFile(file);
            }

            @Override
            public void onLookImageClick(List<ProcessFile> file, int position) {

            }

            @Override
            public void onDelateClick(int position) {
                ivImages.deleteImage(position);
            }
        });

        findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                reqAddInfo.pointCode = etMunber.getText().toString();
                //单提名称
                reqAddInfo.name = etName.getText().toString();
                reqAddInfo.type = et_type.getText().toString();
                //单体级别
                reqAddInfo.monomerLevel = etMonomerLevel.getText().toString();
                reqAddInfo.address = etAddress.getText().toString();
                //基本类型名
                reqAddInfo.baseTypeName = etMonomerLevel.getText().toString();
                //主类名称
                reqAddInfo.mainName = etMainName.getText().toString();
                //亚类名称
                reqAddInfo.subclassName = etSubclassName.getText().toString();
                //开发程度
                reqAddInfo.devLevel = etDevLevel.getText().toString();
                //所属景区
                reqAddInfo.scenicSpot = etScenicSpot.getText().toString();
                //交通状况
                reqAddInfo.traffic = etTraffic.getText().toString();
                //面积
                reqAddInfo.measureArea = etMeasureArea.getText().toString();
                //保护状态
                reqAddInfo.measureStatus = etMeasureStatus.getText().toString();
                //资源图片
                reqAddInfo.picUrl = ivImages.getDataStr();
                //单体介绍
                reqAddInfo.info = etInfo.getText().toString();

                if (TextUtils.isEmpty(reqAddInfo.lat)) {
                    showToast("请定位经纬度坐标！");
                    return;
                }
                getViewModel(TouristModel.class).addInfo(reqAddInfo);
            }
        });

    }

    private void initView() {
        etMunber = (EditText) findViewById(R.id.et_munber);
        et_type= (EditText) findViewById(R.id.et_type);
        etName = (EditText) findViewById(R.id.et_name);
        etMonomerLevel = (EditText) findViewById(R.id.et_monomerLevel);
        etBaseTypeName = (EditText) findViewById(R.id.et_baseTypeName);
        etMainName = (EditText) findViewById(R.id.et_mainName);
        etSubclassName = (EditText) findViewById(R.id.et_subclassName);
        tvLocation = (TextView) findViewById(R.id.tv_location);
        etAddress = (TextView) findViewById(R.id.et_address);
        etDevLevel = (EditText) findViewById(R.id.et_devLevel);
        etScenicSpot = (EditText) findViewById(R.id.et_scenicSpot);
        etTraffic = (EditText) findViewById(R.id.et_traffic);
        etMeasureArea = (EditText) findViewById(R.id.et_measureArea);
        etMeasureStatus = (EditText) findViewById(R.id.et_measureStatus);
        ivImages = (ImageSelectProgressView) findViewById(R.id.iv_images);
        etInfo = (EditText) findViewById(R.id.et_info);
        tvOk = (TextView) findViewById(R.id.tv_ok);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1010) {
                List<String> images = ImageSelector.onActivityResult(resultCode, data);

                if (images != null && images.size() > 0) {
                    ivImages.onUpLoadStart(images);
                }
                String[] imageFile = new String[images.size()];
                images.toArray(imageFile);
                getViewModel(TouristModel.class).onFile(imageFile);
            } else if (requestCode == 1002) {
                double latitude = data.getDoubleExtra("Latitude", 0.0);
                double longitude = data.getDoubleExtra("Longitude", 0.0);
                String address = data.getStringExtra("Address");

                reqAddInfo.lat = latitude + "";
                reqAddInfo.lng = longitude + "";
                reqAddInfo.cityName = data.getStringExtra("City");
                reqAddInfo.cityCode = data.getStringExtra("CityCode");
                reqAddInfo.district = data.getStringExtra("District");
                reqAddInfo.districtCode = data.getStringExtra("AdCode");
                reqAddInfo.township = data.getStringExtra("Township");
                reqAddInfo.townshipCode = data.getStringExtra("Towncode");

                tvLocation.setText(latitude + "," + longitude);
                etAddress.setText(address);

            }
        }

    }

    @LiveDataMatch
    public void onFileSuccess(boolean success, String path, String url) {
        if (success) {
            ivImages.onUpLoadFileSuccess(path, url);
        } else {
            ivImages.onUpLoadFileError(path);
        }
    }

    @LiveDataMatch
    public void onSuccess() {
        DialogBuilder dialogBuilder = new DialogBuilder();
        dialogBuilder.setCancelable(false);
        dialogBuilder.setHideCancel(true);
        dialogBuilder.setMessage("上报成功！");
        dialogBuilder.setOkListener((dialog, which) -> {
            dialog.dismiss();
            finish();
        });
        showDialog(dialogBuilder);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!PermissionUtil.isOpenGPS(this)) {
            // 未打开GPS or 没有GPS 定位权限
            DialogBuilder dialogBuilder = new DialogBuilder();
            dialogBuilder.setMessage("打开GPS 定位权限!");
            dialogBuilder.setHideCancel(true);
            dialogBuilder.setCancelable(false);
            dialogBuilder.setOkListener(new DialogBuilder.DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogBuilder.DialogInterface dialog, int which) {
                    dialog.dismiss();
                    PermissionUtil.openGPS(AddInfoActivity.this);
                }
            });

            showDialog(dialogBuilder);
        }


    }


}
